import tkinter as tk
from tkinter import Canvas


class FlowchartApp:
    def __init__(self, root):
        self.root = root
        self.root.title("可拖拽流程图")

        self.canvas = Canvas(root, width=800, height=600)
        self.canvas.pack()

        self.create_flowchart()

    def create_flowchart(self):
        start = self.canvas.create_oval(50, 50, 150, 100, fill='lightgreen', tags='start')
        process = self.canvas.create_rectangle(200, 50, 350, 100, fill='lightblue', tags='process')
        end = self.canvas.create_oval(400, 50, 500, 100, fill='lightcoral', tags='end')

        self.canvas.create_line(150, 75, 200, 75, arrow=tk.LAST)  # Connect start to process
        self.canvas.create_line(350, 75, 400, 75, arrow=tk.LAST)  # Connect process to end

        self.make_draggable(start)
        self.make_draggable(process)
        self.make_draggable(end)

    def make_draggable(self, item):
        def on_drag(event):
            self.canvas.move(item, event.x - self.canvas.coords(item)[0] - 50,
                             event.y - self.canvas.coords(item)[1] - 25)

        self.canvas.tag_bind(item, '<ButtonPress-1>', lambda e: self.canvas.bind('<Motion>', on_drag))
        self.canvas.tag_bind(item, '<ButtonRelease-1>', lambda e: self.canvas.unbind('<Motion>'))


if __name__ == "__main__":
    root = tk.Tk()
    app = FlowchartApp(root)
    root.mainloop()